package cn.edu.hust.cm.vs;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.util.Pair;

import com.commit451.nativestackblur.NativeStackBlur;

import jp.wasabeef.glide.transformations.internal.FastBlur;
import jp.wasabeef.glide.transformations.internal.RSBlur;

/**
 * Created by kingcmchen on 2016/12/13.
 */

public class BlurImages {
    private static final String TAG = "BlurImages";

    public static final String BLUR_NONE = "NoneBlur";
    public static final String BLUR_FAST = "FastBlur";
    public static final String BLUR_RS = "RsBlur";
    public static final String BLUR_STACK = "StackBlur";

    public final static String[] IMAGE_SET = {
            "http://i.imgur.com/rFLNqWI.jpg",
            "http://i.imgur.com/C9pBVt7.jpg",
            "http://i.imgur.com/rT5vXE1.jpg",
            "http://i.imgur.com/aIy5R2k.jpg",
            "http://i.imgur.com/MoJs9pT.jpg",
            "http://i.imgur.com/S963yEM.jpg",
            "http://i.imgur.com/rLR2cyc.jpg",
            "http://i.imgur.com/SEPdUIx.jpg",
            "http://i.imgur.com/aC9OjaM.jpg",
            "http://i.imgur.com/76Jfv9b.jpg",
            "http://i.imgur.com/fUX7EIB.jpg",
            "http://i.imgur.com/syELajx.jpg",
            "http://i.imgur.com/COzBnru.jpg",
            "http://i.imgur.com/Z3QjilA.jpg",
            "http://baidu.fsad/",
            "http://campusx-10046755.image.myqcloud.com/931c56f7-5c52-4db3-bf1d-14b7db3df5c6",
            "http://campusx-10046755.image.myqcloud.com/9fdf2add-2fe0-47f5-9e97-08c38b017ca3",
            "http://campusx-10046755.image.myqcloud.com/75e82a9b-bcb1-476c-a514-4bc107a2e67c",
            "http://campusx-10046755.image.myqcloud.com/7a52c03c-c080-43e8-90cc-a26c458875ee",
            "http://campusx-10046755.image.myqcloud.com/44c2d479-6828-4e22-8997-37958308d9e5",
            "http://campusx-10046755.image.myqcloud.com/683ddd9d-28e8-4680-b1d1-c703a02e6169",
            "http://campusx-10046755.image.myqcloud.com/96c8d0a8-2952-41cc-b808-adc298c0fdc4",
            "http://campusx-10046755.image.myqcloud.com/a5f5a696-2766-4338-9008-1dcbbd000e4c",
            "http://campusx-10046755.image.myqcloud.com/44c5f696-c8b1-49e0-8ae4-57b356206c87",
            "http://campusx-10046755.image.myqcloud.com/96c8d0a8-2952-41cc-b808-adc298c0fdc4",
            "http://campusx-10046755.image.myqcloud.com/44c5f696-c8b1-49e0-8ae4-57b356206c87",
            "http://campusx-10046755.image.myqcloud.com/a5f5a696-2766-4338-9008-1dcbbd000e4c",
            "http://campusx-10046755.image.myqcloud.com/a1f5c6e4-18a2-4838-915a-0bd0de11bb78",
            "http://campusx-10046755.image.myqcloud.com/6a169162-1b4d-403e-acb9-59348b45dc2d",
            "http://campusx-10046755.image.myqcloud.com/c1aace3f-5a6c-438d-b9f8-c62ce4c81694",
            "http://image57.360doc.com/DownloadImg/2012/12/1116/28841018_14.jpg",
            "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1209/10/c1/13758581_1347257278695.jpg",
            "http://img04.tooopen.com/images/20121122/tooopen_201211221810434692.jpg",
            "http://pic49.nipic.com/file/20140926/17961491_122014331000_2.jpg",
            "http://image.tianjimedia.com/uploadImages/2013/284/F879R8LSSQ28.jpg",
            "http://pic45.nipic.com/20140730/12005913_100105395000_2.jpg",
            "http://pic62.nipic.com/file/20150303/17961491_092446597000_2.jpg",
            "http://pic48.nipic.com/file/20140909/17961491_082407720000_2.jpg",
            "http://img2.3lian.com/2014/c7/29/d/23.jpg",
            "http://image5.tuku.cn/pic/wallpaper/fengjing/langmandushiweimeisheyingkuanpingbizhi/006.jpg",
            "http://pic54.nipic.com/file/20141126/17961491_093029437000_2.jpg",
            "http://image72.360doc.com/DownloadImg/2014/05/0402/41292510_3.jpg",
            "http://h5.86.cc/walls/20150922/1024x768_0cce092c00c8dec.jpg",
            "http://pic41.nipic.com/20140531/1812578_155037556122_2.jpg",
            "http://a0.att.hudong.com/11/04/300224654811132504045236607_950.jpg",
            "http://pic51.nipic.com/file/20141023/17961491_140345410000_2.jpg",
            "http://t10.baidu.com/it/u=4043754453,763123347&fm=32&img.jpg",
            "http://pic65.nipic.com/file/20150421/9885883_114750128000_2.jpg",
            "http://pic38.nipic.com/20140305/15121266_203342815000_2.jpg"
    };

    public static Pair<Bitmap, String> doFastBlur(Bitmap bitmap) {
        long t = System.currentTimeMillis();
        Bitmap blur;

        try {
            blur = FastBlur.blur(bitmap, 25, false);
        } catch (Throwable e) {
            Log.e(TAG, "doFastBlur: ", e);
            return Pair.create(bitmap, "FastBlur: error");
        }

        long cost = System.currentTimeMillis() - t;
        return Pair.create(blur, "FastBlur: 耗时" + cost + "ms");
    }

    public static Pair<Bitmap, String> doRsBlur(Context context, Bitmap bitmap) {
        long t = System.currentTimeMillis();
        Bitmap blur;

        int mSampling = 1;
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);

        try {
            blur = RSBlur.blur(context, bmp, 25);
        } catch (Throwable e) {
            Log.e(TAG, "doRsBlur: ", e);
            return Pair.create(bitmap, "RsBlur: error");
        }

        long cost = System.currentTimeMillis() - t;
        return Pair.create(blur, "RSBlur: 耗时" + cost + "ms");

    }

    public static Pair<Bitmap, String> doStackBlur(Bitmap bitmap) {
        long t = System.currentTimeMillis();
        Bitmap blur;

        try {
            blur = NativeStackBlur.process(bitmap, 25);
        } catch (Throwable e) {
            Log.e(TAG, "doStackBlur: ", e);
            return Pair.create(bitmap, "StackBlur: error");
        }

        long cost = System.currentTimeMillis() - t;
        return Pair.create(blur, "StackBlur: 耗时" + cost + "ms");
    }

    public static Pair<Bitmap, String> doNoneBlur(Bitmap bitmap) {
        return Pair.create(bitmap, "NoneBlur: original image");
    }
}
